<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\controller\Queue;
use app\common\library\Weixin;
use app\common\model\Ad;
use app\common\model\AgentShop;
use app\common\model\Commission;
use app\common\model\OrderGroup;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
	    //代理数量
	    $agentNumber = Db::name('user')
		    ->where('level', '>', 0)
		    ->count();
	
	
	    //设置
	    if($this->area == \app\admin\model\User::AREA_OTHER) {
		    $setting = Db::name('setting')
			    ->find();
	    } else {
		    $setting = Db::name('ruleSz')->find();
	    }
	    $model = new Ad();
	    $this->success('请求成功', [
		    'agent_number' => $setting['show_join_number'] ? $agentNumber+99762 : 0,
		    'setting' => $setting,
		    'banner' => $model->getBannerById(1),
		    'theme' => $model->getBannerById(2),
		    'active' => $model->getBannerById(5),
		    'agent_bg' => $model->getBannerById(3)
	    ]);
    }
	

    
    /**
     * 系统设置
     */
    public function setting() {
    	if($this->area == \app\admin\model\User::AREA_OTHER) {
		    $setting = Db::name('setting')
			    ->find();
	    } else {
    		$setting = Db::name('ruleSz')->find();
	    }
	
	    $this->success('请求成功', $setting);
    }
    
    /**
     * 提货点
     */
    public function agent() {
    	$lat = $this->request->post('lat');
	    $lng = $this->request->post('lng');
	
	    $userModel = new \app\admin\model\User();
	    $agents = Db::name('agentShop')
		    ->where(['role' => ['in', [$userModel::LEVEL_WAREHOUSE, $userModel::LEVEL_SHOP]], 'area' => $this->area, 'status' => AgentShop::STATUS_Y])
		    ->field('id, name, image, content, mobile, lat, lng, address, role, ROUND(6378.138 * 2 * ASIN(SQRT( POW( SIN( ('.$lat.' * PI() / 180 - lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(lat * PI() / 180) * POW( SIN(('.$lng.' * PI() / 180 - lng * PI() / 180 ) / 2 ),  2))) * 1000) AS distance')
		    ->order('distance asc')
		    ->select();
	    
	    if($agents) {
	    	foreach ($agents as &$agent) {
	    		$agent['distance'] = get_distance($agent['distance']);
		    }
	    }
	    $this->success('查询成功', $agents);
    }
    
//    public function sendTemp() {
//    	$model = new OrderGroup();
//    	$res = $model->sendGroupTemplate('19', 'success');
//    	dump($res);
//    }




public function importOldShip() {
    	$orders = Db::name('agentShipment')->alias('s')
		    ->join('agentShop a', 's.sign_agent_id = a.id', 'left')
		    ->order(['s.create_time' => 'asc'])
		    ->field('s.*, a.area')
		    ->select();
	
//	dump($orders);
	if($orders) {
    		$data = [];
    		foreach ($orders as $item) {
    			if(!isset($data[$item['no']])) {
    				$data[$item['no']] = [
					    'area' => $item['area'],
					    'no' => $item['no'],
					    'sign_agent_id' => $item['sign_agent_id'],
					    'send_agent_id' => $item['send_agent_id'],
					    'shipping_name' => $item['ship_name'],
					    'shipping_no' => $item['ship_no'],
					    'create_time' => $item['create_time'],
					    'update_time' => $item['update_time'],
					    'status' => $item['status'],
					    'items' => []
				    ];
			    }
    			
    			$data[$item['no']]['items'][] = $item;
		    }
    		
    		foreach ($data as $item) {
    			$order = Db::name('shipOrder')
				    ->insert([
					    'area' => $item['area'],
					    'no' => $item['no'],
					    'sign_agent_id' => $item['sign_agent_id'],
					    'send_agent_id' => $item['send_agent_id'],
					    'postman_id' => '',
					    'shipping_name' => $item['shipping_name'],
					    'shipping_no' => $item['shipping_no'],
					    'send_code' => mt_rand(1000, 9999),
					    'sign_code' => mt_rand(1000, 9999),
					    'create_time' => $item['create_time'],
					    'update_time' => $item['update_time'],
					    'status' => $item['status'],
				    ]);
    			
    			$orderId =Db::name('shipOrder')->getLastInsID();
    			$items = [];
    			foreach ($item['items'] as $goods) {
				    $items[] = [
					    'order_id' => $orderId,
					    'goods_id' => $goods['goods_id'],
					    'spec_id' => $goods['spec_id'],
					    'number' => $goods['number']
				    ];
			    }
    			
    			Db::name('shipItem')->insertAll($items);
			   
		    }
	    }
}
}
